<?php

namespace app\api\controller;

use app\api\service\TokenService;
use app\mpbase\library\WechatCard;
use app\common\model\User as UserModel;
use think\Controller;
use think\Db;

class User extends Base
{
    public function decrypt()
    {
        $wechatApp = new \app\mpbase\library\WechatApp();
        $res = $wechatApp->decryptData();
        return self::apiResp($res);
    }

    public function login_code()
    {
        $wechatApp = new \app\mpbase\library\WechatApp();
        $sessionData = $wechatApp->code();
//        $sessionData = action('mpbase/WechatApp/code');
        $res = (new UserModel())->loginByOpenId($sessionData['openid']);
        return self::apiResp($res);
    }


    public function check()
    {
        $token = input('token');
        $check = (new TokenService())->check($token);
        $res = [
            'check' => $check,
            'msg' => $check ? '校验通过' : '校验失败',
        ];
        return self::apiResp($res);
    }

    //检测用户是否在 公众号的关注列表里
    public function checkUserList($id)
    {
        $applet = Db::table('uct_wechat_applet')->where('id',get_api_applet_id())->find();
        if(!$applet['is_follow']){ //不进行验证
            $res = ['_code' => 200, 'msg' => '用户已关注公众号，可以领取'];
        }else {
            $user = new UserModel();
            $mp_openid = $user->where(['id' => $id])->find()['mp_openid'];
            $userlist = $user->getUserList();
            if (in_array($mp_openid, $userlist)) {//存在用户列表内
                $res = ['_code' => 200, 'msg' => '用户已关注公众号，可以领取'];
            } else {//不存在用户列表内
                $res = ['_code' => 400, 'msg' => '用户尚未关注公众号，不可领取'];
            }
        }
        return self::apiResp($res);
    }
}
